

import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lz_app_core/constants/app_manager.dart';
import 'package:lz_app_core/utils/kd_storage.dart';
import 'package:lz_app_core/utils/sp_util.dart';
import 'package:lz_ui/kd_ui.dart';

class KDPrivatePolicyAlert extends StatelessWidget {
  KDPrivatePolicyAlert({Key? key, this.onTapAgree}) : super(key: key);

  Function()? onTapAgree;

  static const privatePolicyStateKey = "privatePolicyStateKey";

  @override
  Widget build(BuildContext context) {
    return Container(
      width: KD.screenW *2/3,
      color: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text.rich(
            TextSpan(
              children: [
                const TextSpan(
                  text: '你需要同意',
                ),
                TextSpan(
                  text: '《用户服务协议》',
                  style: KDTextStyle.text_16.copyWith(color: KDColor.main),
                  recognizer: TapGestureRecognizer()..onTap = (){
                    userProtocal();
                  }
                ),
                const TextSpan(
                  text: '和',
                ),
                TextSpan(
                    text: '《隐私政策》',
                    style: KDTextStyle.text_16.copyWith(color: KDColor.main),
                    recognizer: TapGestureRecognizer()..onTap = (){
                      priveProtocal();
                    }
                ),
                const TextSpan(
                  text: '才能继续使用本应用,否则我们无法正常向您提供服务。',
                ),
              ]
            ),
          ),
          SizedBox(height: 32.kdw,),
          KDButton.bottomBtn(title: '同意并进入', tap: (){
            onTapAgree?.call();
            KDStorage.saveBool(privatePolicyStateKey, true);
          },),
          SizedBox(height: 24.kdw,),
          GestureDetector(
            child: Text('不同意并退出', style: KDTextStyle.text_14.copyWith(color: KDColor.place),),
            onTap: (){
              exit(0);
            },
          ),
        ],
      ),
    );
  }

  /// 用户协议
  userProtocal() {
    KDWebViewPage page = KDWebViewPage.webPage(
        title: "用户服务协议", url: AppManager.webUrlModel.userlPrototal);
    Get.to(() => page);
  }

  /// 隐私协议
  priveProtocal() {
    KDWebViewPage page = KDWebViewPage.webPage(
        title: "隐私政策", url: AppManager.webUrlModel.priveProtocal);
    Get.to(() => page);
  }

  static showPrivatePolicy({Function()? onTapAgree}) async{

    bool? agree = KDStorage.get<bool>(privatePolicyStateKey);
    if(agree == true) {
      return onTapAgree?.call();
    }

    showDialog(context: KDAlert.navContext, builder: (context) => AlertDialog(
      title: const Center(child: Text('隐私政策')),
      content: KDPrivatePolicyAlert(onTapAgree: onTapAgree,),
    ), barrierDismissible: false);
  }
}
